package cn.iocoder.yudao.module.bpm.service.oa.listener;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.springframework.stereotype.Component;

/**
 * @ClassName BpmOALeaveSendWeChatMessageListener
 * @Description 给审批人发送企业微信消息
 * @Author hanjiale
 * @Date 2024/5/12 21:43
 * @Version 1.0
 */
@Slf4j
@Component
public class BpmOALeaveSendWeChatMessageListener implements JavaDelegate {

    @Override
    public void execute(DelegateExecution execution) {
        log.info("[execute][execution({}) 被调用！变量有：{}，活动id{},实例id{}]", execution.getId(),
                execution.getCurrentFlowableListener().getFieldExtensions(),execution.getCurrentActivityId(),execution.getProcessInstanceId());
        System.out.println("业务："+ execution.getProcessInstanceBusinessKey());
        String token = "MXRD0Lm0klCCpgNlaAeBMc0XngFZ9SUqOEO8Pin4NQRf7UVOuJn6josV1SGmPnBuuNoteZU2VdJjniLZA5YDb2mRtZv9aI47uY5mg5S_m_ypWXbD9v2F1yQryPGTwvewLXxz6gEEeEmL1jrTODE0DKlqPy5YNh1sfEp0b-FHqrYeAn05zXaui2MNrI6T-6rA2NjZ-jl7fw-GgRTotgFxXg";

        Message message = new Message();
        message.setTouser("hanjiale");
        message.setMsgtype("text");
        message.setAgentid(1000030);

        Text text = new Text();
        text.setContent("你的快递已到，请携带工卡前往邮件中心领取。\n出发前可查看<a href=\"http://localhost:80\">需要审批的流程</a>，聪明避开排队。");
        message.setText(text);
        final String body = HttpRequest.post("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + token+"&debug=1")
                .body(JSONUtil.toJsonStr(message),"application/json; charset=utf-8").execute().body();
        System.out.println("结果：" + body);
    }
    @Data
    class Message {
        private String touser;
        private String toparty;
        private String totag;
        private String msgtype;
        private int agentid;
        private Text text;
        private int safe;
        private int enable_id_trans;
        private int enable_duplicate_check;
        private int duplicate_check_interval;

    }
    @Data
    class Text {
        private String content;
    }

}
